<template>
    <div class="home">
        <div class="search-container">
            <div class="search-box">
                <div class="title">智能问答</div>
                <div class="animated-bubble">
                    <div class="bubble-content">
                        <span>💬</span>
                        <span>AI助手</span>
                    </div>
                </div>
                <!-- <div style="display: flex;justify-content: center;">
                    <div class="empty-chat-text">HI~ 别来无恙</div>
                </div> -->
                <div class="input-wrapper">
                    <el-input class="input_layout" v-model="search" type="textarea" :autosize="{ minRows: 1.4, maxRows: 7 }" :placeholder="`请输入`" @input="searchChange">
                    </el-input>
                    <div class="button-wrapper">
                        <el-button type="primary" @click="handleSearch">
                            <el-icon class="search-icon">
                                <Search />
                            </el-icon>
                            发送
                        </el-button>
                    </div>
                    <SpeechInput ref="stopimport" @speech-result="handleSpeechResult" />
                </div>

            </div>
            <div class="hot-question-list" v-if="questionList.length > 0">
                <!-- <div > -->
                <div class="hot-question-item" v-for="(item, index) in questionList" :key="index" @click="sendTipQuestion(item)" style=" cursor: pointer;">
                    <span style="margin: 0 5px 0 0;">
                        {{ index + 1 }} .
                    </span>
                    <span class="hot-question-item-title">
                        {{ item }}
                    </span>
                </div>
                <!-- </div> -->
            </div>

        </div>
    </div>
</template>

<script setup>
import { ref, reactive, defineEmits, onMounted, onUnmounted, nextTick, computed } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import { getQuestion, getAnswer } from "@/api/index.js"
import SpeechInput from '@/components/SpeechInput.vue'

const emit = defineEmits(['search'])
const router = useRouter()
const route = useRoute()

// 输入框
const search = ref(``)
// 防抖初始值
let debounceTimer = null;
// 联想词汇
let questionList = ref([])


// 输入框监听事件
const searchChange = (e) => {
    if (search.value !== '') {
        clearTimeout(debounceTimer);
        debounceTimer = setTimeout(() => {
            getQuestionList();
        }, 500);
    }
};
// 联想词汇
const getQuestionList = () => {
    getQuestion({ question: search.value }).then(res => {
        if (res.data == []) {
            questionList.value.push('没有找到相关问题，请重新输入')
        } else {
            questionList.value = res.data
        }
    })
}
// 选择联想词汇
const sendTipQuestion = (item) => {
    search.value = item
    handleSearch()
}
// 检查内容是否为空白
const isEmptyContent = (str) => {
    return !str || str.trim().length === 0;
};
const handleSearch = () => {
    // 再次检查内容是否为空或仅包含空白字符
    if (isEmptyContent(search.value)) return;
    emit('search', {
        val: search.value,
    })
    search.value = '' // 清空输入框
    stopimportchange()
}
let stopimport = ref();
// 暂停输入
const stopimportchange = () => {
    stopimport.value.stopimportfun()
}
// 处理语音识别结果
const handleSpeechResult = (result) => {
    if (result === '') {
        // 如果是空字符串，说明是开始新的录音，清空输入框
        search.value = ''
    } else {
        // 否则追加识别结果
        search.value += result
        getQuestionList()
    }

}

</script>

<style lang="scss" scoped>
.home {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    // background: linear-gradient(180deg, #c1defd 0%, #ffffff 100%);
    background: linear-gradient(180deg, #eef2ff 0%, #fafbff 100%);

    .search-container {
        text-align: center;
        padding: 48px;
        border-radius: 16px;
        background: rgba(255, 255, 255, 0.9);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);

        .search-box {
            width: 800px;

            .title {
                font-size: 36px;
                font-weight: bold;
                color: #333;
                margin-bottom: 32px;
                letter-spacing: 2px;
            }

            .animated-bubble {
                user-select: none;
                position: relative;
                width: 190px;
                height: 45px;
                border-radius: 30px;
                background: rgba(255, 255, 255, 0.9);
                border: 2px solid rgba(102, 126, 234, 0.3);
                display: flex;
                align-items: center;
                justify-content: center;
                margin: 0 auto;
                margin-bottom: 20px;
                animation: bubbleFloat 3s ease-in-out infinite;
                box-shadow: 0 4px 16px rgba(102, 126, 234, 0.2);
                // cursor: pointer;
                transition: all 0.3s ease;

                &:hover {
                    transform: scale(1.05);
                    box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
                    border-color: rgba(102, 126, 234, 0.5);
                }

                &::before {
                    content: "";
                    position: absolute;
                    top: -2px;
                    left: -2px;
                    right: -2px;
                    bottom: -2px;
                    background: linear-gradient(
                        45deg,
                        rgba(102, 126, 234, 0.2),
                        rgba(118, 75, 162, 0.2),
                        rgba(102, 126, 234, 0.2)
                    );
                    border-radius: 32px;
                    z-index: -1;
                    animation: bubbleGlow 2s ease-in-out infinite;
                }

                .bubble-content {
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                    display: flex;
                    align-items: center;
                    gap: 8px;
                    animation: bubblePulse 2s ease-in-out infinite;

                    span:first-child {
                        font-size: 24px;
                        animation: emojiBounce 1.5s ease-in-out infinite;
                    }

                    span:last-child {
                        font-size: 16px;
                        font-weight: 600;
                        color: #667eea;
                    }
                }
            }

            .input-wrapper {
                position: relative;
                // width: calc(100% - 48px);
                max-width: 800px;
                // max-height: 300px;
                border-radius: 16px;
                border: 2px solid rgba(182, 170, 255, 0.9411764706);
                background: rgba(255, 255, 255, 0.8);
                box-sizing: border-box;
                z-index: 10;
                min-height: 120px !important;
                padding: 10px 10px 50px 10px;

                :deep(.el-textarea__inner) {
                    padding: 5px 16px 16px 5px;
                    font-size: 16px;
                    line-height: 1.6;
                    // border: 2px solid #b6aafff0;
                    border-radius: 12px;
                    background: rgba(255, 255, 255, 0.9);
                    box-shadow: none;
                    resize: none;
                    // 自定义滚动条样式
                    &::-webkit-scrollbar {
                        width: 4px;
                        height: 4px;
                    }

                    &::-webkit-scrollbar-thumb {
                        background-color: rgba(144, 147, 153, 0.3);
                        border-radius: 4px;

                        &:hover {
                            background-color: rgba(144, 147, 153, 0.5);
                        }
                    }

                    &::-webkit-scrollbar-track {
                        background: transparent;
                    }

                    // Firefox 滚动条样式
                    scrollbar-width: thin;
                    scrollbar-color: rgba(144, 147, 153, 0.3) transparent;
                }

                .button-wrapper {
                    position: absolute;
                    right: 16px;
                    bottom: 16px;

                    .el-button {
                        background: #409eff;
                        border-color: #409eff;
                        padding: 12px 24px;
                        font-size: 16px;
                        border-radius: 8px;
                        transition: all 0.3s;

                        &:hover {
                            background: #5240d0;
                            transform: translateY(-1px);
                        }

                        .search-icon {
                            margin-right: 8px;
                            font-size: 18px;
                        }
                    }
                    .model-selector {
                        display: flex;
                        align-items: center;
                        gap: 4px;
                        padding: 6px 12px;
                        border-radius: 6px;
                        font-size: 14px;
                        color: #333;
                        background: #f5f7fa;
                        transition: all 0.3s;

                        &:hover {
                            background: #e9ecf2;
                        }

                        .el-icon {
                            font-size: 12px;
                            color: #909399;
                            transition: transform 0.3s;
                        }
                    }
                }

                .cardselectboxs {
                    position: absolute;
                    left: 16px;
                    bottom: 8px;
                    display: flex;
                    align-items: center;
                }
                .cardselect {
                    // position: absolute;
                    // left: 16px;
                    // bottom: 8px;
                    display: flex;
                    margin: 0 5px 0 0;
                    .cardselectbox {
                        user-select: none;
                        cursor: pointer;
                        padding: 8px;
                        background: #dec8f0;
                        border-radius: 15px;
                        color: #ffffff;
                    }
                }
            }
        }
        // 热门问题列表
        .hot-question-list {
            height: 370px;
            overflow-x: auto;
            margin: 16px 0;
            display: grid;
            gap: 12px;
            // position: absolute;
            // left: 16px;
            // bottom: 8px;
            .hot-question-item {
                display: flex;
                align-items: center;
                text-align: left;
                padding: 10px 16px;
                background: #f8fafc;
                border-radius: 12px;
                transition: all 0.2s ease;
                border: 1px solid transparent;
                cursor: pointer;

                .question-index {
                    display: flex;
                    align-items: center;
                    margin-right: 12px;
                    color: #64748b;

                    .el-icon {
                        margin-right: 6px;
                        font-size: 16px;
                        color: #4f46e5;
                    }

                    span {
                        font-size: 14px;
                        font-weight: 500;
                    }
                }

                .hot-question-item-title {
                    flex: 1;
                    font-size: 14px;
                    color: #1e293b;
                    line-height: 1.5;
                    letter-spacing: 0.01em;
                }

                .el-icon {
                    color: #94a3b8;
                    margin-left: 12px;
                    transition: all 0.2s ease;
                }

                &:hover {
                    background: #eef2ff;
                    border-color: #e0e7ff;
                    // transform: translateX(4px);

                    .hot-question-item-title {
                        color: #4f46e5;
                    }

                    .el-icon {
                        color: #4f46e5;
                        // transform: translateX(4px);
                    }
                }
            }
        }
    }
}
.mic-button {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    user-select: none;
    border: 2px solid #eee;
    position: relative;
}

.mic-icon {
    font-size: 24px;
    transition: all 0.3s ease;
}

// 动画气泡的关键帧动画
@keyframes bubbleFloat {
    0%,
    100% {
        transform: translateY(0px);
    }
    50% {
        transform: translateY(-10px);
    }
}

@keyframes bubbleGlow {
    0%,
    100% {
        opacity: 0.5;
        transform: scale(1);
    }
    50% {
        opacity: 0.8;
        transform: scale(1.05);
    }
}

@keyframes bubblePulse {
    0%,
    100% {
        transform: translate(-50%, -50%) scale(1);
    }
    50% {
        transform: translate(-50%, -50%) scale(1.05);
    }
}

@keyframes emojiBounce {
    0%,
    100% {
        transform: translateY(0px);
    }
    50% {
        transform: translateY(-3px);
    }
}
</style>